<?php require_once('Connections/learnConn.php');

if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$colname_nilai = $_GET['nis'];
  $colname_nilai1 = $_GET['smt'];
  $colname_nilai2 = $_GET['ta'];

mysql_select_db($database_learnConn, $learnConn);
$query_nilai = sprintf("SELECT * FROM nilai WHERE nis=%s AND tahun_ajaran=%s AND semester=%s", 
					   GetSQLValueString($colname_nilai, "text"),
					   GetSQLValueString($colname_nilai2, "text"),
					   GetSQLValueString($colname_nilai1, "text"));

$nilai = mysql_query($query_nilai, $learnConn) or die(mysql_error());
$row_nilai = mysql_fetch_assoc($nilai);
$totalRows_nilai = mysql_num_rows($nilai);
$rata = 0;

$query_nama = sprintf("SELECT * FROM siswa WHERE nis=%s", 
					   GetSQLValueString($colname_nilai, "text"));

$nama = mysql_query($query_nama, $learnConn) or die(mysql_error());
$row_siswa = mysql_fetch_assoc($nama);

#setting judul laporan dan header tabel
if ($totalRows_nilai !=0) {
	$judul = "Nilai ".$row_siswa['nama']." Untuk Tahun Ajaran ".$_GET['ta']." Semester ".$_GET['smt'];
} else {
	$judul = "Hasil Untuk Nilai ".$_GET['nis']." Tidak Ditemukan Untuk Tahun Ajaran ".$_GET['ta'];
}
//$judul = "Nilai Siswa";
$header = array(
		array("label"=>"Mata Pelajaran", "length"=>80, "align"=>"L"),
		array("label"=>"Ulangan", "length"=>40, "align"=>"L"),
		array("label"=>"Tugas", "length"=>30, "align"=>"L"),
		array("label"=>"UTS", "length"=>30, "align"=>"L")
	);	
#sertakan library FPDF dan bentuk objek
require_once ("fpdf/fpdf.php");
$pdf = new FPDF('p','mm','A4');
$pdf->AddPage(); 
#tampilkan judul laporan
$pdf->SetFont('Arial','','11');
$pdf->Cell(0,10, $judul, '0', 1, 'L'); 
#buat header tabel
$pdf->SetFont('Arial','','10');
$pdf->SetTextColor(0);
//$pdf->SetDrawColor(128,0,0);
//foreach ($header as $kolom) {
	//$pdf->Cell($kolom['length'], 5, $kolom['label'], 1, '0', $kolom['align'], true);
//}
$pdf->SetFillColor(220,220,220);
$pdf->Cell(40, 10, 'Mata Pelajaran', 1, '0', 'C', true);
$pdf->SetFillColor(0,255,0);
$pdf->Cell(50, 5, 'Ulangan', 1, '0', 'C', true);
$pdf->SetFillColor(0,180,255); //blue
$pdf->Cell(50, 5, 'Tugas', 1, '0', 'C', true);
$pdf->SetFillColor(220,220,220);
$pdf->Cell(15, 10, 'UTS', 1, '0', 'C', true);
$pdf->SetFillColor(220,220,220);
$pdf->Cell(15, 10, 'UAS', 1, '0', 'C', true);
$pdf->SetFillColor(220,220,220);
$pdf->Cell(20, 10, 'Nilai Akhir', 1, '0', 'C', true);
$pdf->Cell(1, 5, '', 0, '0', 'C', false); //tambahan

$pdf->Ln();
$pdf->Cell(40, 5, '', 0, '0', 'C', false);
$pdf->SetFillColor(0,255,0);
$pdf->Cell(10, 5, '1', 1, '0', 'C', true);
$pdf->Cell(10, 5, '2', 1, '0', 'C', true);
$pdf->Cell(10, 5, '3', 1, '0', 'C', true);
$pdf->Cell(10, 5, '4', 1, '0', 'C', true);
$pdf->Cell(10, 5, '5', 1, '0', 'C', true);
$pdf->SetFillColor(0,180,255); //blue
$pdf->Cell(10, 5, '1', 1, '0', 'C', true);
$pdf->Cell(10, 5, '2', 1, '0', 'C', true);
$pdf->Cell(10, 5, '3', 1, '0', 'C', true);
$pdf->Cell(10, 5, '4', 1, '0', 'C', true);
$pdf->Cell(10, 5, '5', 1, '0', 'C', true);
$pdf->Cell(15, 5, '', 0, '0', 'C', false);
$pdf->Cell(15, 5, '', 0, '0', 'C', false);
$pdf->Cell(20, 5, '', 0, '0', 'C', false);

#tampilkan data tabelnya
$pdf->SetFillColor(224,235,255);
$pdf->SetTextColor(0);
$pdf->SetFont('Arial','','9');
//$pdf->Ln();
$pdf->Ln();
$rata = 0;
do { 
$colname_matpell = $row_nilai['kd_matpel'];
		mysql_select_db($database_learnConn, $learnConn);
		$query_matpell = sprintf("SELECT * FROM matpel WHERE kd_matpel = %s", GetSQLValueString($colname_matpell, "text"));
		$matpell = mysql_query($query_matpell, $learnConn) or die(mysql_error());
		$row_matpell = mysql_fetch_assoc($matpell);
		$totalRows_matpell = mysql_num_rows($matpell);
		//echo $row_matpell['nama_matpel'];
	//echo $row_nilai['ul1'];
	$pdf->SetFillColor(255,255,200);
	$pdf->Cell(40, 6, $row_matpell['nama_matpel'], 1, '0', 'L', true);
	//$pdf->Cell(12, 5, $row_matpell['ul1'], 1, '0', 'C', true);
	$pdf->SetFillColor(200,255,200);
	$pdf->Cell(10, 6, $row_nilai['ul1'], 1,'0','C',true);
	$pdf->Cell(10, 6, $row_nilai['ul2'], 1,'0','C',true);
	$pdf->Cell(10, 6, $row_nilai['ul3'], 1,'0','C',true);
	$pdf->Cell(10, 6, $row_nilai['ul4'], 1,'0','C',true);
	$pdf->Cell(10, 6, $row_nilai['ul5'], 1,'0','C',true);
	$pdf->SetFillColor(200,220,255);
	$pdf->Cell(10, 6, $row_nilai['tgs1'], 1,'0','C',true);
	$pdf->Cell(10, 6, $row_nilai['tgs2'], 1,'0','C',true);
	$pdf->Cell(10, 6, $row_nilai['tgs3'], 1,'0','C',true);
	$pdf->Cell(10, 6, $row_nilai['tgs4'], 1,'0','C',true);
	$pdf->Cell(10, 6, $row_nilai['tgs5'], 1,'0','C',true);
	$pdf->SetFillColor(255,255,200);
	$pdf->Cell(15, 6, $row_nilai['uts'], 1, '0', 'C', true);
	$pdf->Cell(15, 6, $row_nilai['uas'], 1, '0', 'C', true);
	$pdf->Cell(20, 6, $row_nilai['nilai_akhir'], 1, '0', 'C', true);
	//$pdf->Cell(100,10,'Pembuatan File PDF',1,1,'R');	
	$rata = $rata + $row_nilai['nilai_akhir'];
	$pdf->Ln();
	
} while ($row_nilai = mysql_fetch_assoc($nilai));	
	$total = 'Rata-rata     =';
	
	$ratarata=0;
	if($totalRows_nilai != 0) {
	$ratarata = $rata / $totalRows_nilai; 
	$ratarata = number_format($ratarata , 2); 
	} else {
		//echo $ratarata;
	}
	  
	$pdf->Cell(40,10,'',0,0,'L'); 
	$pdf->Cell(50,10,'',0,0,'C');
	$pdf->Cell(50,10,'',0,0,'C');
	$pdf->Cell(15,10,'',0,0,'C');
	$pdf->Cell(15,10,$total,0,0,'C');	
	$pdf->Cell(20,10,$ratarata,0,0,'C');	
	
/*
foreach ($data as $baris) {
	$i = 0;
	foreach ($baris as $cell) {
		$pdf->Cell($header[$i]['length'], 5, $cell, 1, '0', $kolom['align'], $fill);
		$i++;
	}
	$fill = !$fill;
	$pdf->Ln();
}
*/
#output file PDF
$pdf->Output();
?>
</body>
</html>